﻿using Dyao.Book.Entity;
using Dyao.Book.Repository.Abstractions;

namespace Dyao.Book.Repository
{
    using System.Collections.Generic;
    using Foundation.Replacement;

    public class BookMatchSourceRepository : DataRepository<BookMatchSourceEntity>, IBookMatchSourceRepository
    {
        /// <inheritdoc cref="IBookMatchSourceRepository.GetEntityByBookIdAsync"/>
        public async Task<BookMatchSourceEntity> GetEntityByBookIdAsync(string bookId, string sourceKey, CancellationToken cancellationToken)
        {
            var query = this.GetQueryable()
                .Where(c => c.BookId == bookId)
                .Where(c => c.SourceKey == sourceKey);

            return await query.FirstOrDefaultAsync(cancellationToken);
        }

        /// <inheritdoc cref="IBookMatchSourceRepository.GetListByBookIdAsync"/>
        public async Task<List<BookMatchSourceEntity>> GetListByBookIdAsync(string bookId, CancellationToken cancellationToken)
        {
            var query = this.GetQueryable()
                .Where(c => c.BookId == bookId);

            return await query.ToListAsync(cancellationToken);
        }
    }
}
